package com.example.yiba8.base;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.yiba8.util.ServerResources;
import android.app.Fragment ;

/**
 * Created by ${zyj} on 2016/1/14.
 */
public abstract class BaseFragment extends Fragment {

    private View fragmentRootView  ;

    //Fragment当前状态是否可见
    protected boolean isVisible;
    private int lazyLoadNumber = 0 ;



    //接口管理
    public ServerResources serverResources ;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {  //可见
            isVisible = true;
            onVisible();
        } else {   //不可见
            isVisible = false;
            onInvisible();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //这里可以做一些 公共资源初始化的工作
        serverResources = new ServerResources() ;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentRootView  = inflaterView(inflater, container, savedInstanceState);
        initView(fragmentRootView) ;
        initDataFromThread();        //耗时操作
        return fragmentRootView  ;
    }

    /**
     * 设置布局
     * @param inflater
     * @param container
     * @param bundle
     * @return
     */
    public abstract View inflaterView(LayoutInflater inflater,
                                      ViewGroup container, Bundle bundle);

    /**
     * 初始化控件
     * @param fragmentRootView
     */
    public abstract void initView( View fragmentRootView ) ;

    /**
     * 耗时操作
     */
    public abstract void initDataFromThread();

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoadNumber ++ ;
        lazyLoad( lazyLoadNumber );
    }

    /**
     * 不可见
     */
    protected void onInvisible() {

    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad( int number );
}
